const express = require('express');
const router = express.Router();
const db = require('../config/database');

router.get('/list', (req, res) => {
    // 查询所有
    db.query('select * from tb_user', (err, data) => {
        res.send({
            msg: '查询成功',
            status: true,
            data
        })
    })
})

// 根据id查找
router.get('/findById', (req, res) => {
    let id = req.query.id
    db.query('select * from tb_user where id = ?', id, (err, data) => {
        res.send({
            msg: '查询成功',
            status: true,
            data
        })
    })
})

// 添加用户
router.post('/add', (req, res) => {
    let userInfo = req.body
    db.query('insert into tb_user set ?', userInfo, (err, data) => {
        res.send({
            msg: '添加成功',
            status: true,
            data
        })
    })
})

// 修改用户
router.post('/update', (req, res) => {
    let userInfo = req.body
    db.query('update tb_user set ? where id = ?', [userInfo, userInfo.id], (err, data) => {
        res.send({
            msg: '修改成功',
            status: true,
            data
        })
    })
})


module.exports = router